package com.example.demo.controller;

import com.example.demo.domain.Goods;
import com.example.demo.domain.Picture;
import com.example.demo.service.GoodsService;
import com.example.demo.utils.FileUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/upload")
public class UploadController {

    @Value("${ip}")
    private String ip;
    @Value("${server.port}")
    private String port;
    @Value("${uploadGoodsPath}")
    private String uploadGoodsPath;
    @Value("${uploadIconPath}")
    private String uploadIconPath;
    @Value("${goodsPicPathPattern}")
    private String goodsPicPathPattern;
    @Value("${iconPathPattern}")
    private String iconPathPattern;
    @Resource
    private GoodsService goodsService;
    /**
     * @Create:     LT4
     * @Time:       2020年6月13日21:12:10
     * @Describe:   解析excel文件
     */
    @PostMapping("/excel")
    public String uploadExcel(MultipartFile file) throws IOException {
        String fileName=file.getOriginalFilename();
        if(fileName.substring(fileName.lastIndexOf(".")+1).equals("xlsx")||fileName.substring(fileName.lastIndexOf(".")+1).equals("xls"))
        {
            List<Object> list= FileUtils.getExcelData(file);
            for(Object goods : list){
                System.out.println(goods);
                //使用Goodsservice 添加到数据库中
//            goodsService.addGoods((Goods) goods,null);
            }
        }
        return "redirect:/goods";
    }

    /**
     * @Create:     Space
     * @Time:       2020-6-13 22:55:00
     * @Describe:   上传商品信息
     */
    @RequestMapping("/uploadGoods")
    @ResponseBody
    public ModelAndView uploadGoods(@RequestParam(value = "file",required = false) MultipartFile[] file,
                                    Goods good,@RequestParam(value = "path",required = false) String path,@RequestParam("state")Integer state) throws Exception
    {
        System.out.println(path);
        System.out.println(good);
        System.out.println(state);
        ModelAndView mv=new ModelAndView();
        if(file!=null&&file.length>0)
        {
            List<String> fileName =new ArrayList<String>();
            PrintWriter out = null;
            try
            {
                for (int i = 0; i < file.length; i++)
                {
                    if (!file[i].isEmpty())
                    {
                        fileName.add(FileUtils.uploadImage(uploadGoodsPath, ip,port,file[i], true));
                    }
                }
                if(state==1)
                {
                    goodsService.saveGoods(good,fileName);
                }
                else
                {
                    goodsService.addGoods(good,fileName);
                }
                //上传成功
                if(fileName!=null&&fileName.size()>0)
                {
                    System.out.println("上传成功");
                }
                else
                {
                    System.out.println("无图片");
                }
            }
            catch (Exception e)
            {
                e.printStackTrace();
                System.out.println("上传异常");
            }
        }
        else
        {
            System.out.println("上传异常");
        }
        mv.setViewName("redirect:http://"+ip+":"+port+path);
        return mv;
    }

}
